package cn.iocoder.yudao.module.call.controller.admin.ivrflowbranchstat;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.call.controller.admin.ivrflowbranchstat.vo.*;
import cn.iocoder.yudao.module.call.dal.dataobject.ivrflow.IvrFlowBranchStatDO;
import cn.iocoder.yudao.module.call.service.ivrflowbranchstat.IvrFlowBranchStatService;

@Tag(name = "管理后台 - 节点分支命中百分比")
@RestController
@RequestMapping("/call/ivr-flow-branch-stat")
@Validated
public class IvrFlowBranchStatController {

    @Resource
    private IvrFlowBranchStatService ivrFlowBranchStatService;

    @PostMapping("/create")
    @Operation(summary = "创建节点分支命中百分比")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<Long> createIvrFlowBranchStat(@Valid @RequestBody IvrFlowBranchStatSaveReqVO createReqVO) {
        return success(ivrFlowBranchStatService.createIvrFlowBranchStat(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新节点分支命中百分比")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:update')")
    public CommonResult<Boolean> updateIvrFlowBranchStat(@Valid @RequestBody IvrFlowBranchStatSaveReqVO updateReqVO) {
        ivrFlowBranchStatService.updateIvrFlowBranchStat(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除节点分支命中百分比")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:delete')")
    public CommonResult<Boolean> deleteIvrFlowBranchStat(@RequestParam("id") Long id) {
        ivrFlowBranchStatService.deleteIvrFlowBranchStat(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得节点分支命中百分比")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<IvrFlowBranchStatRespVO> getIvrFlowBranchStat(@RequestParam("id") Long id) {
        IvrFlowBranchStatDO ivrFlowBranchStat = ivrFlowBranchStatService.getIvrFlowBranchStat(id);
        return success(BeanUtils.toBean(ivrFlowBranchStat, IvrFlowBranchStatRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得节点分支命中百分比分页")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<PageResult<IvrFlowBranchStatRespVO>> getIvrFlowBranchStatPage(@Valid IvrFlowBranchStatPageReqVO pageReqVO) {
        PageResult<IvrFlowBranchStatDO> pageResult = ivrFlowBranchStatService.getIvrFlowBranchStatPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IvrFlowBranchStatRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得节点分支命中百分比")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<List<IvrFlowBranchStatRespVO>> getIvrFlowBranchStatList(@Valid IvrFlowBranchStatPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<IvrFlowBranchStatDO> pageResult = ivrFlowBranchStatService.getIvrFlowBranchStatPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult.getList(), IvrFlowBranchStatRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出节点分支命中百分比 Excel")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:export')")
    @OperateLog(type = EXPORT)
    public void exportIvrFlowBranchStatExcel(@Valid IvrFlowBranchStatPageReqVO pageReqVO,
                                             HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IvrFlowBranchStatDO> list = ivrFlowBranchStatService.getIvrFlowBranchStatPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "节点分支命中百分比.xls", "数据", IvrFlowBranchStatRespVO.class,
                BeanUtils.toBean(list, IvrFlowBranchStatRespVO.class));
    }

}